#include "head.h"
extern sqlite3 *db;

void do_login(int sockfd, MSG *pbuf)
{
	char *errmsg;
	char sql[64] = {'\0'};
	char **user_msg;
	int nrow,ncolumn;
	printf("这是新的登录\n");
	sprintf(sql,"select * from data where name = '%s';",pbuf->name);
	//数据库查询
	if( sqlite3_get_table(db,sql,&user_msg,&nrow,&ncolumn,&errmsg) == SQLITE_OK)
	{
		if(nrow != 0 && ncolumn != 0)
		{
			printf("get this user\n");
			if(strcmp(user_msg[9],pbuf->passwd) != 0)
			{
				printf("passwd error\n");
				pbuf->ret = -1;
				strcpy(pbuf->data,"密码错误");
			}else
			{
		    		printf("login ok\n");
		    		//查询成功 ret = 0，data = “登陆成功”
	    			pbuf->ret = atoi(user_msg[15]);
		   	 	strcpy(pbuf->data,"login ok");
			}
		}
		else 
		{
			printf("no this user\n");
			//查询失败 ret = -1, data = "失败“
			pbuf->ret = -1;
			strcpy(pbuf->data,"用户名错误");
		}
	}
	else
	{
		printf("find error\n");
		//查询失败 ret = -1, data = "登录失败“
		pbuf->ret = -1;
		strcpy(pbuf->data,"查找失败");
	}
	//将登录的结果反馈给用户端
	send(sockfd,pbuf,sizeof(MSG),0);	
	printf("re send %s,%d\n",pbuf->data,pbuf->ret);
}

void do_staff_change_passwd(int sockfd, MSG *pbuf)
{
	char *errmsg;
	char sql[64] = {'\0'};
	sprintf(sql,"update data set passwd = '%s' where name = '%s';",pbuf->data,pbuf->name);
	printf("%s\n",sql);
	// 根据 name 修改数据库内容为 passwd
    if( sqlite3_exec(db,sql,NULL,NULL,&errmsg) == SQLITE_OK)
	{
		//修改成功 ret = 0，data = “修改密码完成”
		pbuf->ret = 0;
		strcpy(pbuf->data,"修改密码完成");
	}
	else 
	{
		//查询失败 ret = -1, data = "修改密码失败“
		pbuf->ret = -1;
		strcpy(pbuf->data,"修改密码失败");
	}
	send(sockfd,pbuf,sizeof(MSG),0);
}

void do_staff_query(int sockfd, MSG *pbuf)
{
	//查找用户信息
	char *errmsg;
	char sql[64] = {'\0'};
	char **user_msg;
	int  nrow,ncolumn;
	sprintf(sql,"select * from data where name = '%s';",pbuf->name);

    if( sqlite3_get_table(db,sql,&user_msg,&nrow,&ncolumn,&errmsg) == SQLITE_OK)
	{
		//查询成功 ret = 0，查询到的信息保存在user_msg中
		//数据库顺序： name,passwd,id,trueName,addr,age,salary,level 
		pbuf->ret = 0;
		strcpy(pbuf->passwd,user_msg[9]);
		pbuf->info.id = atoi(user_msg[10]);
		strcpy(pbuf->info.name,user_msg[11]);
		strcpy(pbuf->info.addr,user_msg[12]);
		pbuf->info.age = atoi(user_msg[13]);
		pbuf->info.salary = atof(user_msg[14]);
		pbuf->ret = atoi(user_msg[15]);
	}
	else 
	{
		//查询失败 ret = -1, data = "获得员工信息失败“
		pbuf->ret = -1;
		strcpy(pbuf->data,"获得员工信息成失败");
	}
    //发送 info
	send(sockfd,pbuf,sizeof(MSG),0);
}

void do_admin_query(int sockfd, MSG *pbuf)
{
    	// 若 data为空 发送当前用户信息
	// 若 data不为空 查询用户并将此用户的信息发送
	printf("admin query began\n");
	if(strcmp(pbuf->data," ") == 0)
	{
		do_staff_query(sockfd,pbuf);
	}
	else
	{
	   printf("began adimin quert \n");
	    char *errmsg;
	    char sql[64] = {'\0'};
	    char **user_msg;
	    int  nrow,ncolumn;
	    sprintf(sql,"select * from data where name ='%s';",pbuf->data);

        if( sqlite3_get_table(db,sql,&user_msg,&nrow,&ncolumn,&errmsg) == SQLITE_OK)
  	    {
		    //查询成功 ret = 0，查询到的信息保存在user_msg中
		    //数据库顺序： name,passwd,id,trueName,addr,age,salary,level 
	  	if(nrow !=0 && ncolumn != 0)
		{ 
	   	    printf("adimin quert seccuss\n");
		    strcpy(pbuf->passwd,user_msg[9]);
		    pbuf->info.id = atoi(user_msg[10]);
		    strcpy(pbuf->info.name,user_msg[11]);
		    strcpy(pbuf->info.addr,user_msg[12]);
		    pbuf->info.age = atoi(user_msg[13]);
		    pbuf->info.salary = atof(user_msg[14]);
		    pbuf->ret = atoi(user_msg[15]);
		}
		else
		{
		    pbuf->ret = -1;
		    strcpy(pbuf->data,"无此用户");
			
		}
	    }
	    else 
	    {
		    //查询失败 ret = -1, data = "获得员工信息失败“
		    pbuf->ret = -1;
		    strcpy(pbuf->data,"获得员工信息成失败");
	    }
        //发送 infoi
	    send(sockfd,pbuf,sizeof(MSG),0);
	    printf("find resend %s,%s\n",pbuf->data,pbuf->passwd);
    }
}

void do_admin_del(int sockfd, MSG *pbuf){
	//查询data用户 从数据库删除
	char *errmsg;
	char sql[64] = {'\0'};
	sprintf(sql,"delete from data where name = '%s';",pbuf->data);
	//数据库删除操作
	if( sqlite3_exec(db,sql,NULL,NULL,&errmsg) == SQLITE_OK)
	{
		//删除成功 ret = 0，data = “删除成功”
		pbuf->ret = 0;
		strcpy(pbuf->data,"删除成功");
	}
	else 
	{
		//删除失败 ret = -1, data = "删除失败“
		pbuf->ret = -1;
		strcpy(pbuf->data,"删除成功");
	}
	//将登录的结果反馈给用户端
	send(sockfd,pbuf,sizeof(MSG),0);
}

void do_admin_add(int sockfd, MSG *pbuf){
	char *errmsg;
	char sql[128] = {'\0'};
	char **user_msg;
	int nrow = 0,ncolumn = 0;
	sprintf(sql,"select * from data where name = '%s';",pbuf->data);
   	sqlite3_get_table(db,sql,&user_msg,&nrow,&ncolumn,&errmsg);
  	if(nrow !=0 || ncolumn != 0)
	{
		pbuf->ret = -1;
		strcpy(pbuf->data,"此用户名已被注册");
	}
	else
	{
	    sprintf(sql,"insert into data(name,passwd,id,trueName,addr,age,salary,level) values('%s','%s',%d,'%s','%s',%d,%f,%d);",pbuf->data,pbuf->passwd,pbuf->info.id,\
			pbuf->info.name,pbuf->info.addr,pbuf->info.age,\
			pbuf->info.salary,pbuf->ret);
		if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) == SQLITE_OK)
		{
			pbuf->ret = 0;
			strcpy(pbuf->data,"添加成功");
		}else
		{
		    pbuf->ret = -1;
			strcpy(pbuf->data,"添加失败");
		};
	}
	send(sockfd,pbuf,sizeof(MSG),0);
}

void do_admin_changeInfo(int sockfd,MSG *pbuf)
{
	char *errmsg;
	char sql[128] = {'\0'};
	sprintf(sql,"update data set passwd = '%s',id = %d,trueName = '%s',addr = '%s',age = %d,salary = %f,level = %d where name = '%s';",\
		pbuf->passwd,pbuf->info.id,pbuf->info.name,pbuf->info.addr,pbuf->info.age,\
		pbuf->info.salary,pbuf->ret,pbuf->data);
	printf("%s\n",sql);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) == SQLITE_OK)
	{
		pbuf->ret = 0;
		strcpy(pbuf->data,"修改成功");
	}else
	{
		pbuf->ret = -1;
		strcpy(pbuf->data,"修改失败");
	};
	
	send(sockfd,pbuf,sizeof(MSG),0);
}
